<!--
* description:客户信息查询
* author:shensai
* createdDate: 2021-1-25
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="false"
      :dynamic-is-show-more-btn="true"
    />
    <personaledit
      :key="personalPopupsKey"
      :popups-visible.sync="personalPopupsVisible"
      :dynamic-edit-row-data="editRowData"
      :popups-state="editPopupsState"
    />
    <companyedit
      :key="companyPopupsKey"
      :popups-visible.sync="companyPopupsVisible"
      :dynamic-edit-row-data="editRowData"
      :popups-state="editPopupsState"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import { custApi } from '@/api/se/customerManagement.js'
import OneTableTemplate from '@/components/templates/oneTable'
import { exportExcel } from '@/utils/getTimeString'
import personaledit from './personaledit'
import companyedit from './companyedit'
import Vue from 'vue'
export default {
  name: 'CustomerInformation',
  components: {
    OneTableTemplate,
    personaledit,
    companyedit
  },
  mixins: [oneTableWithViewTemplateMixins],
  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData(
      [to.path, 'supplierMessage-edit'],
      () => {
        next()
      }
    )
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      personalPopupsKey: '1',
      personalPopupsVisible: false,

      editRowData: {},
      companyPopupsKey: '1',
      companyPopupsVisible: false,

      // 网格查询API配置对象
      apiConfig: custApi.customerInformationQuery,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          clickEvent: () => this.queryTable(),
          text: this.$t('sys.button.query'), // 查询
          name: 'search', // 按钮图标样式
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey4',
          size: 'small',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          name: 'reset',
          position: 'right' // 重置
        },
        {
          compKey: 'comKey6',
          size: 'small',
          name: 'export',
          position: 'left',
          clickEvent: () => this.exportToExcel(),
          text: '导出'
        }
      ],
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            // 显示组件
            {
              compKey: 'compKey1',
              labelName: '客户姓名：',
              codeField: 'custName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '联系电话：',
              codeField: 'phone',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '客户类型：',
              codeField: 'custClassCode',
              component: () => import('@/components/org/LookupValue'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '2', statusText: '企业' },
                { statusId: '1', statusText: '个人' }
              ],
              type: 'dropdownList',
              // isMul: true,
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '客户性别：',
              codeField: 'genderCode',
              component: () => import('@/components/org/LookupValue'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '1', statusText: '男' },
                { statusId: '2', statusText: '女' }
              ],
              type: 'dropdownList',
              // isMul: true,
              isMust: true
            },
            {
              compKey: 'compKey5',
              labelName: '客户属性：',
              // codeField: 'custType',
              codeField: 'property',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'lookupValueCode,lookupValueName',
              customObj: {
                api: custApi.customerAttributes,
                selectType: '1'
              },
              isMust: false
            }
          ],
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'controlBtn1',
              label: '操作',
              minWidth: 100,
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey1',
                  labelName: '查看',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: index => this.edit(index)
                }
              ]
            },
            {
              prop: 'custNo',
              label: '客户编号'
            },
            {
              prop: 'custName',
              label: '客户姓名'
            },
            {
              prop: 'phone',
              label: '联系电话'
            },
            {
              prop: 'custClassName',
              label: '客户类型'
            },
            {
              prop: 'gender',
              label: '性别'
            },
            {
              prop: 'propertyName',
              label: '客户属性'
            },
            {
              prop: 'credTypeName',
              label: '证件类型'
            },
            {
              prop: 'credNo',
              label: '证件号'
            },
            {
              prop: 'addrStreet',
              label: '客户地址'
            }
          ],
      formField: {
        custName: '',
        phone: '',
        custClassCode: '',
        genderCode: '',
        property: ''// 客户属性
      }
    }
  },
  mounted() {
    this.queryTable(1)
  },
  methods: {
    exportToExcel() {
      exportExcel(
        this.formField,
        custApi.customerDetailsExport.APIUrl,
        custApi.customerDetailsExport.ServiceCode,
        '客户信息'
      )
    },

    edit(index) {
      this.editRowData = this.$utils.deepCopy(
        this.$refs['multipleTable'].list[index]
      )
      if (this.editRowData.custClassCode === '1') { // 个人
        // debugger
        this.personalPopupsVisible = true
      } else {
        this.companyPopupsVisible = true
      }
    }
  }
}
</script>
